﻿using AutoMapper;
using MediatR;
using Notification.API.Application.Controller.Notification;
using Notification.API.DTOs.Notifications;
using Notification.API.Utils;
using Notification.Domain;
using Notification.Errorcode.ErrorCode;
using Notification.Infrasturcture.Interfaces;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;

namespace Notification.API.Application.CommandHandler.Notifications
{
    public class GetNotificationCommandHandle : IRequestHandler<GetIdNotificationCommand, ApiResult<PagingResult<IList<NotificationDTOs>>>>
    {
        private readonly INotificationRepository notificationRepository;
        private readonly IDepartmentRepository departmentRepository;
        private readonly IRelateProjectRepository relateProjectRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetNotificationCommandHandle> _logger;

        public GetNotificationCommandHandle(INotificationRepository notificationRepository, ILogger<GetNotificationCommandHandle> logger, IMapper mapper, IDepartmentRepository departmentRepository, IRelateProjectRepository relateProjectRepository)
        {
            this.notificationRepository = notificationRepository; // 修正：使用 this 关键字
            _logger = logger;
            _mapper = mapper;
            this.departmentRepository = departmentRepository;
            this.relateProjectRepository = relateProjectRepository;
        }

        public async Task<ApiResult<PagingResult<IList<NotificationDTOs>>>> Handle(GetIdNotificationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //获取所有的到货检验信息
                var list = (await notificationRepository.GetAllAsync()).AsQueryable();
                ////外键 获取部门信息
                //var department = await departmentRepository.GetAllAsync();
                //// 外键 获取关联项目
                //var relateProject = await relateProjectRepository.GetAllAsync();

                //var result = from a in list
                //             join b in department on a.Departmentid equals b.Departmentid
                //             join c in relateProject on a.Relatedprojectid equals c.Relatedprojectid
                //             select new NotificationDTOs
                //             {
                //                 Productid = a.Productid,
                //                 Qualityinspection = a.Qualityinspection,
                //                 Inspectionnumber = a.Inspectionnumber,
                //                 Inspectiondate = a.Inspectiondate,
                //                 Inspectionpeople = a.Inspectionpeople,
                //                 Departmentid = a.Departmentid,
                //                 Inspectionphone = a.Inspectionphone,
                //                 Remarks = a.Remarks,
                //                 Purchasenumber = a.Purchasenumber,
                //                 Purchasedate = a.Purchasedate,
                //                 Suppliername = a.Suppliername,
                //                 Purchasepeople = a.Purchasepeople,
                //                 Purchasephone = a.Purchasephone,
                //                 Purchasingstaff = a.Purchasingstaff,
                //                 Purchasingdepartment = a.Purchasingdepartment,
                //                 Contactinformation = a.Contactinformation,
                //                 Relatedprojectid = a.Relatedprojectid,
                //                 Productoverview = a.Productoverview,
                //             };

                if (!string.IsNullOrEmpty(request.Qualityinspection))
                {
                    list = list.Where(x => x.Qualityinspection.Contains(request.Qualityinspection));
                }
                if (!string.IsNullOrEmpty(request.Inspectionnumber))
                {
                    list = list.Where(x => x.Inspectionnumber.Contains(request.Inspectionnumber));
                }
                if (request.Inspectiondate != null)
                {
                    list = list.Where(x => x.Inspectiondate == request.Inspectiondate);
                }
                if (!string.IsNullOrEmpty(request.Inspectionpeople))
                {
                    list = list.Where(x => x.Inspectionpeople.Contains(request.Inspectionpeople));
                }
                if (!string.IsNullOrEmpty(request.Purchasenumber))
                {
                    list = list.Where(x => x.Purchasenumber.Contains(request.Purchasenumber));
                }
                // 当前页的数据
                var lists = _mapper.Map<IList<NotificationDTOs>>(list.Skip(request.pagesize * (request.pageIndex - 1)).Take(request.pagesize).ToList());


                // 分页
                var pagingResult = new PagingResult<IList<NotificationDTOs>>
                {
                    TotalCount = list.Count(),
                    TotalPage = Math.Ceiling(list.Count() / (double)request.pagesize),
                    Data = lists
                };

                // 返回成功结果
                return ApiResult<PagingResult<IList<NotificationDTOs>>>.Success(pagingResult, StatusCodeEnum.Success);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}